<template>
    <el-form class="formRef" :model="formdata" label-width="120px">
        <el-form-item label="代码名称">
            <el-input v-model="formdata.name"></el-input>
        </el-form-item>
        <el-form-item label="代码描述">
            <el-input v-model="formdata.description"></el-input>
        </el-form-item>
        <el-form-item label="代码">
            <MonaoEditor id="monaco" :value="formdata.context" @change="change"></MonaoEditor>
        </el-form-item>
        <el-form-item v-show="error" label="代码错误">
            <pre>{{ error }}</pre>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus';
import { onMounted, ref, watch, watchEffect } from 'vue';
import { asynccreateorupdatespecialjudge, asyncselectspecialjudge } from '../../api';
import Studycodemodel from '../../mysql_interface/studycodemodel';
import MonaoEditor from '../MonaoEditor.vue'

const prop = defineProps<{ ID?: number, change: (ID: number) => void, show: boolean }>()
const formdata = ref<Studycodemodel.Ispecialjudge>({
    name: 'spj',
    description: '',
    context: `#include "testlib.h"
#include <string>
// 目前只支持 testlib库 
// 选手正确返回 quitf(_ok, '')
// testlib 的github https://github.com/MikeMirzayanov/testlib
using namespace std;
int main(int argc, char *argv[])
{
    /*
   * inf：输入
   * ouf：选手输出
   * ans：标准输出
   */
    registerTestlibCmd(argc, argv);
    string output = ouf.readString();
    string Answer = ans.readString();
    if(output != Answer)
        quitf(_wa, "wrong");
    quitf(_ok, "right");
}`
})


const error = ref('')

function change(context?: string, e?: any) {
    formdata.value.context = context
}


const olddata = ref<Studycodemodel.Ispecialjudge>()


async function submit() {
    try {
        error.value = ''
        const data = await asynccreateorupdatespecialjudge(formdata.value)
        if (data.ID) {
            prop.change(data.ID)
        }
        formdata.value = data
        ElMessage.success('提交成功')
    } catch (e: any) {
        ElMessage.error('编译失败')
        error.value = e
    }
}

defineExpose({
    submit
})


watch(() => prop.ID, (e) => {
    start()
})

async function start() {
    if (prop.ID) {
        const data = await asyncselectspecialjudge({
            ID: prop.ID
        })
        const [t] = data.rows
        if (t) {
            formdata.value = t
            olddata.value = t
        } else {
            ElMessage.error('没有查找到')
        }
    } else {
        formdata.value = {
            name: 'spj',
            description: '',
            context: `#include "testlib.h"
#include <string>
// 目前只支持 testlib库 
// 选手正确返回 quitf(_ok, '')
// testlib 的github https://github.com/MikeMirzayanov/testlib
using namespace std;
int main(int argc, char *argv[])
{
    /*
   * inf：输入
   * ouf：选手输出
   * ans：标准输出
   */
    registerTestlibCmd(argc, argv);
    string output = ouf.readString();
    string Answer = ans.readString();
    if(output != Answer)
        quitf(_wa, "wrong");
    quitf(_ok, "right");
}`
        }
    }

}

onMounted(() => {
    start()
})

</script>


<style scoped>
.formRef {
    margin-bottom: 30px;
}

#monaco {
    border: 1px solid #ccc;
    height: 500px;
    width: 100%;
}
</style>
